package com.jshoperxms.controller.mall.backstage.sysrole;

import com.jshoperxms.Response;
import com.jshoperxms.controller.mall.backstage.base.BaseTController;
import com.jshoperxms.controller.mall.backstage.base.IBaseCtrl;
import com.jshoperxms.controller.mall.backstage.base.SearchModel;
import com.jshoperxms.controller.utils.BaseTools;
import com.jshoperxms.controller.utils.DateTools;
import com.jshoperxms.controller.utils.enums.BaseEnums;
import com.jshoperxms.controller.utils.enums.SysRoleEnums;
import com.jshoperxms.controller.utils.statickey.SerialKeys;
import com.jshoperxms.controller.utils.statickey.TagWords;
import com.jshoperxms.controller.utils.statickey.WarnWrods;
import com.jshoperxms.entity.GoodsTypeT;
import com.jshoperxms.entity.SysModuleActionT;
import com.jshoperxms.entity.SysModuleT;
import com.jshoperxms.service.SysModuleActionTService;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by sdywcd on 16/6/23.
 * Des:系统模块功能控制器
 */
@Controller
@RequestMapping("/mall/sysmoduleaction")
public class SysModuleActionTController extends BaseTController implements IBaseCtrl<SysModuleActionT>{

    @Resource
    private SysModuleActionTService sysModuleActionTService;
    //数据形式
    private Map<String, Object> responsedata = new HashMap<>();




    /**
     * 保存系统模块功能
     * @param rq
     * @return
     */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public ResponseEntity save(@RequestBody SysModuleActionT rq){
        if(StringUtils.isNotBlank(rq.getSysmoduleid())&&StringUtils.isNotBlank(rq.getSysmoduleactionname())){
            if(checkDuplicates(rq.getSysmoduleactionname())){
                SysModuleActionT bean=new SysModuleActionT();
                bean.setId(this.serial.Serialid(SerialKeys.SYSMODULEACTIONT));
                bean.setActionurl(rq.getActionurl());
                bean.setCreatetime(DateTools.getSystemTimeBi());
                bean.setCreatorid(BaseTools.getAdminCreateId());
                bean.setSort(1);
                bean.setStatus(BaseEnums.DataUsingState.USING.getState());
                bean.setSysmodulename(rq.getSysmodulename());
                bean.setSysmoduleid(rq.getSysmoduleid());
                bean.setSysmoduleactionname(rq.getSysmoduleactionname());
                bean.setType(rq.getType());
                bean.setUpdatetime(DateTools.getSystemTimeBi());
                bean.setVersiont(1);
                bean.setResourcetype(rq.getResourcetype());
                bean.setPermission(rq.getPermission());
                this.sysModuleActionTService.save(bean);
                responsedata.put(TagWords.SUCCESS, true);
                responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
            }else{
                responsedata.put(TagWords.SUCCESS,false);
                responsedata.put(TagWords.MSG,WarnWrods.ERROR_HAVE_KEY);

            }
        }else{
            responsedata.put(TagWords.SUCCESS,false);
            responsedata.put(TagWords.MSG,WarnWrods.ERROR_PARAMS);
        }
        return Response.success(responsedata);
    }


    @RequestMapping(value="/find",method= RequestMethod.POST)
    @Override
    public ResponseEntity find(@RequestBody SysModuleActionT rq) {
        if(StringUtils.isBlank(rq.getId())){
            responsedata.put(TagWords.SUCCESS,false);
            responsedata.put(TagWords.MSG,WarnWrods.ERROR_PARAMS);
        }else{
            SysModuleActionT bean=this.sysModuleActionTService.findByPK(SysModuleActionT.class, rq.getId());
            if(bean!=null){
                responsedata.put(TagWords.BEAN,bean);
                responsedata.put(TagWords.SUCCESS,true);
                responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
            }else{
                responsedata.put(TagWords.SUCCESS,false);
                responsedata.put(TagWords.MSG,TagWords.INVALIDDATA);
            }
        }
        return Response.success(responsedata);
    }


    @RequestMapping(value="/del",method= RequestMethod.POST)
    @Override
    public ResponseEntity del(@RequestBody SysModuleActionT rq) {
        if(StringUtils.isBlank(rq.getId())){
            responsedata.put(TagWords.SUCCESS,false);
            responsedata.put(TagWords.MSG,WarnWrods.ERROR_PARAMS);
        }else{
            String ids[]=StringUtils.split(rq.getId(), TagWords.SPLITDOT);
            for(String s:ids){
                SysModuleActionT bean=this.sysModuleActionTService.findByPK(SysModuleActionT.class, s);
                if(bean!=null){
                    bean.setStatus(BaseEnums.DataUsingState.DEL.getState());
                    bean.setUpdatetime(DateTools.getSystemTimeBi());
                    this.sysModuleActionTService.update(bean);
                }
            }
            responsedata.put(TagWords.SUCCESS,true);
            responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
        }
        return Response.success(responsedata);
    }


    //更新模块权限
    @RequestMapping(value="/updateMulitAction",method= RequestMethod.POST)
    public ResponseEntity updateMulitAction(@RequestBody SysModuleActionT rq){
        if(StringUtils.isBlank(rq.getSysmoduleid())||StringUtils.isBlank(rq.getSysmodulename())){
            responsedata.put(TagWords.SUCCESS,false);
            responsedata.put(TagWords.MSG,WarnWrods.ERROR_PARAMS);
        }else{
            this.sysModuleActionTService.saveOrupdateSysModuleActionTransactional(rq);
            responsedata.put(TagWords.SUCCESS,true);
            responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
        }
        return Response.success(responsedata);
    }


    @RequestMapping(value="/update",method= RequestMethod.POST)
    @Override
    public ResponseEntity update(@RequestBody SysModuleActionT rq) {
        if(StringUtils.isBlank(rq.getId())){
            responsedata.put(TagWords.SUCCESS,false);
            responsedata.put(TagWords.MSG,WarnWrods.ERROR_PARAMS);
        }else{
            SysModuleActionT bean=this.sysModuleActionTService.findByPK(SysModuleActionT.class,rq.getId());
            if(bean!=null){
                bean.setActionurl(rq.getActionurl());
                bean.setStatus(rq.getStatus());
                bean.setType(rq.getType());
                bean.setSysmoduleid(rq.getSysmoduleid());
                bean.setSysmodulename(rq.getSysmodulename());
                bean.setSysmoduleactionname(rq.getSysmoduleactionname());
                bean.setUpdatetime(DateTools.getSystemTimeBi());
                bean.setVersiont(bean.getVersiont()+1);
                bean.setPermission(rq.getPermission());
                bean.setResourcetype(rq.getResourcetype());
                this.sysModuleActionTService.update(bean);
                responsedata.put(TagWords.SUCCESS,true);
                responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
            }else{
                responsedata.put(TagWords.SUCCESS,false);
                responsedata.put(TagWords.MSG,TagWords.INVALIDDATA);
            }
        }
        return Response.success(responsedata);
    }


    //根据模块id查询权限
    @RequestMapping(value="/findAllBySysModuleId",method= RequestMethod.POST)
    public ResponseEntity findAllBySysModuleId(@RequestBody SysModuleActionT rq){
        Criterion criterion=Restrictions.eq(TagWords.STATUS, BaseEnums.DataUsingState.USING.getState());
        criterion=Restrictions.and(criterion).add(Restrictions.eq("sysmoduleid",rq.getSysmoduleid()));
        List<SysModuleActionT> list=this.sysModuleActionTService.findByCriteria(SysModuleActionT.class,criterion, Order.desc(TagWords.UPDATETIME));
        responsedata.put(TagWords.BEANLIST,list);
        responsedata.put(TagWords.SUCCESS, true);
        responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
        return Response.success(responsedata);
    }

    @Override
    public ResponseEntity findAll() {
        return null;
    }


    @RequestMapping(value="/findByPage",method= RequestMethod.POST)
    @Override
    public ResponseEntity findByPage(SearchModel searchModel, @RequestParam("search[value]") String search, @RequestParam("order[0][column]") String ordervalue, @RequestParam("order[0][dir]") String ordertype) {
        findCustomSearch(searchModel,search,ordervalue,ordertype);
        return Response.success(responsedata);
    }

    @Override
    public void findCustomSearch(SearchModel searchModel, String search, String ordervalue, String ordertype) {
        if(searchModel.getIsCustomSearch()==1){
            int currentPage=searchModel.getCalCurrentPage();
            int lineSize = searchModel.getLength();
            Criterion criterion=Restrictions.ne(TagWords.STATUS, BaseEnums.DataUsingState.DEL.getState());
            if(StringUtils.isNotBlank(searchModel.getName())){
                criterion=Restrictions.and(criterion).add(Restrictions.like("sysmoduleactionname",searchModel.getName(), MatchMode.ANYWHERE));
            }
            if(StringUtils.isNotBlank(search)){
                criterion=Restrictions.and(criterion).add(Restrictions.like("sysmoduleactionname",search,MatchMode.ANYWHERE));
            }
            int records=this.sysModuleActionTService.count(SysModuleActionT.class,criterion).intValue();
            searchModel.setRecordsTotal(records);
            searchModel.setRecordsFiltered(records);
            responsedata.put(TagWords.RECORDSTOTAL,records);
            responsedata.put(TagWords.RECORDSFILTERED,records);
            List<SysModuleActionT> list = this.sysModuleActionTService.findByCriteriaByPage(
                    SysModuleActionT.class, criterion,Order.desc(TagWords.UPDATETIME), currentPage,
                    lineSize);
            this.processPageList(list);
        }else{
            if(StringUtils.isNotBlank(search)){
                filterSearch(searchModel,search);
            }else{
                findDefaultAll(searchModel,ordervalue,ordertype);
            }
        }
    }

    @Override
    public void filterSearch(SearchModel searchModel, String search) {
        int currentPage=searchModel.getCalCurrentPage();
        int lineSize = searchModel.getLength();
        Criterion criterion;
        Criterion criterion1=Restrictions.ne(TagWords.STATUS, BaseEnums.DataUsingState.DEL.getState());
        Criterion criterion2=Restrictions.like("sysmoduleactionname",search,MatchMode.ANYWHERE);
        criterion=Restrictions.and(criterion1).add(criterion2);
        int records=this.sysModuleActionTService.count(SysModuleActionT.class,criterion).intValue();
        searchModel.setRecordsTotal(records);
        searchModel.setRecordsFiltered(records);
        responsedata.put(TagWords.RECORDSTOTAL,records);
        responsedata.put(TagWords.RECORDSFILTERED,records);
        List<SysModuleActionT> list = this.sysModuleActionTService.findByCriteriaByPage(
                SysModuleActionT.class, criterion,Order.desc(TagWords.UPDATETIME), currentPage,
                lineSize);
        this.processPageList(list);
    }

    @Override
    public void findDefaultAll(SearchModel searchModel, String ordervalue, String ordertype) {
        int currentPage=searchModel.getCalCurrentPage();
        int lineSize = searchModel.getLength();
        Criterion criterion=Restrictions.ne(TagWords.STATUS, BaseEnums.DataUsingState.DEL.getState());
        int records=this.sysModuleActionTService.count(SysModuleActionT.class,criterion).intValue();
        searchModel.setRecordsFiltered(records);
        searchModel.setRecordsTotal(records);
        responsedata.put(TagWords.RECORDSTOTAL,records);
        responsedata.put(TagWords.RECORDSFILTERED,records);
        Order order=BaseTools.orderByParam("sysmoduleactionname",ordervalue,ordertype);
        List<SysModuleActionT> list = this.sysModuleActionTService.findByCriteriaByPage(
                SysModuleActionT.class, criterion,order, currentPage,
                lineSize);
        this.processPageList(list);
    }

    @Override
    public void processPageList(List<SysModuleActionT> list) {
        List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
        for (SysModuleActionT bean:list) {
            Map<String, Object> cellMap = new HashMap<String, Object>();
            cellMap.put("id", bean.getId());
            cellMap.put("sysmoduleactionname", bean.getSysmoduleactionname());
            cellMap.put("actionurl",bean.getActionurl());
            cellMap.put("resourcetype",bean.getResourcetype());
            cellMap.put("permission",bean.getPermission());
            cellMap.put("sysmodulename",bean.getSysmodulename());
            cellMap.put("sysmoduleid",bean.getSysmoduleid());
            cellMap.put("type", SysRoleEnums.TYPE.getName(bean.getType()));
            cellMap.put("status", BaseEnums.DataUsingState.getName(bean.getStatus()));
            cellMap.put("updatetime", DateTools.formateLongDateToString(bean.getUpdatetime().longValue()));
            cellMap.put("version", bean.getVersiont());
            data.add(cellMap);
        }
        responsedata.put(TagWords.DATA, data);
    }

    @Override
    public boolean checkDuplicates(String str) {
        Criterion criterion= Restrictions.eq("sysmoduleactionname",str);
        SysModuleActionT sysModuleActionT=this.sysModuleActionTService.findOneByCriteria(SysModuleActionT.class,criterion);
        if(sysModuleActionT!=null){
            return false;
        }
        return true;
    }
}
